
package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.WorkLog;
import com.sasis.dao.WorkLogDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class WorkLogDaoiBatis extends BaseDaoiBATIS implements WorkLogDao {

    /**
     * @see com.sasis.dao.WorkLogDao#getWorkLogs(com.sasis.model.WorkLog)
     */
    public List getWorkLogs(final WorkLog workLog) {
          return getSqlMapClientTemplate().queryForList("getWorkLogs", workLog);
    }

    /**
     * @see com.sasis.dao.WorkLogDao#getWorkLog(Integer id)
     */
    public WorkLog getWorkLog(Integer id) {
        WorkLog workLog = (WorkLog) getSqlMapClientTemplate().queryForObject("getWorkLog", id);

        if (workLog == null) {
            throw new ObjectRetrievalFailureException(WorkLog.class, id);
        }

        return workLog;
    }

    /**
     * @see com.sasis.dao.WorkLogDao#saveWorkLog(WorkLog workLog)
     */    
    public void saveWorkLog(final WorkLog workLog) {
        Integer id = workLog.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addWorkLog", workLog);
        } else {
            getSqlMapClientTemplate().update("updateWorkLog", workLog);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(WorkLog.class, id);
        }
    }

    /**
     * @see com.sasis.dao.WorkLogDao#removeWorkLog(Integer id)
     */
    public void removeWorkLog(Integer id) {
        getSqlMapClientTemplate().update("deleteWorkLog", id);
    }
}
